package com.jianjian.gulimall.product.service.impl;

import com.jianjian.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.jianjian.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.jianjian.gulimall.product.entity.AttrEntity;
import com.jianjian.gulimall.product.service.AttrAttrgroupRelationService;
import com.jianjian.gulimall.product.service.AttrService;
import com.jianjian.gulimall.product.vo.AttrGroupRelationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianjian.common.utils.PageUtils;
import com.jianjian.common.utils.Query;

import com.jianjian.gulimall.product.dao.AttrGroupDao;
import com.jianjian.gulimall.product.entity.AttrGroupEntity;
import com.jianjian.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationDao relationDao;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 三级查询的分页查询
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        //catelogId为零表示无条件
        if (catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else {
           wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] relationVos) {
        List<AttrAttrgroupRelationEntity> relationEntityList = Arrays.asList(relationVos).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        relationDao.deleteBatchRelation(relationEntityList);
    }

    //自己写的
    @Override
    public PageUtils queryNoAttrPage(Map<String, Object> params, Long attrgroupId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");//检索关键字
//        String order = (String) params.get("order");//排序方式
//        if (!StringUtils.isEmpty(order)){
//          if (order.equalsIgnoreCase("asc")){
//              queryWrapper.orderByAsc("showDesc");
//          }
//            queryWrapper.orderByDesc("showDesc");
//        }
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("attr_id",key).or().like("attr_name",key);
        }

        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        //过滤掉已经添加的
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
//       log.info(attrgroupRelationEntityList.toString());
        if (attrgroupRelationEntityList.size() > 0 ) {
            List<AttrEntity> attrEntityList = page.getRecords();
            List<Long> ids =attrgroupRelationEntityList.stream().map(item ->{
                return item.getAttrId();
            }).collect(Collectors.toList());
//            log.info("ids {}",ids);
                    //判断过滤
                    List<AttrEntity> list = attrEntityList.stream().filter(item ->
                            !ids.contains(item.getAttrId())
                    ).collect(Collectors.toList());
//                    log.info("lis, {}",list);
                    page.setRecords( list);
        }

        return new PageUtils(page);
    }

}